import { TabPanelName, TabsPaneContext } from "element-plus";
import { defineStore } from "pinia";

export const markdown = defineStore("markdown", {
    state: () => {
        return {
            biaoqian: {
                dqm: "", //当前标签页
                dq: 1,
                shuju: [] as any
            },
            wenjian: {
                wenjianjia: [
                    {
                        mingcheng: "文件夹1",
                        biaohao: [1],
                        biaoshi: "[1]",
                        wenjianjia: [
                            {
                                mingcheng: "文件夹1,1",
                                biaohao: [1, 1],
                                biaoshi: "[1,1]",
                                wenjianjia: [
                                    {
                                        mingcheng: "文件夹1,1,1",
                                        biaohao: [1, 1, 1],
                                        biaoshi: "[1,1,1]",
                                        wenjianjia: [],
                                        wenjian: [
                                            {
                                                mingcheng: "文件1,1,1,1",
                                                biaohao: [1, 1, 1, 1],
                                                biaoshi: "[1, 1, 1, 1]",
                                                zhuangtai: false,
                                                wenben: "你可真牛逼",
                                                shijian: 2000 - 1 - 1
                                            }
                                        ]
                                    }
                                ],
                                wenjian: []
                            }
                        ],
                        wenjian: []
                    },
                    {
                        mingcheng: "文件夹2",
                        biaohao: [2],
                        biaoshi: "[2]",
                        wenjianjia: [],
                        wenjian: []
                    }
                ],
                wenjian: [
                    {
                        mingcheng: "文件1",
                        biaohao: [1],
                        biaoshi: "[1]",
                        wenben: "",
                        zhuangtai: false,
                        shijian: 2000 - 1 - 1
                    },
                    {
                        mingcheng: "文件2",
                        biaohao: [2],
                        biaoshi: "[2]",
                        wenben: "",
                        zhuangtai: false,
                        shijian: 2000 - 1 - 1
                    },
                    {
                        mingcheng: "文件3",
                        biaohao: [3],
                        biaoshi: "[3]",
                        wenben: "",
                        zhuangtai: false,
                        shijian: 2000 - 1 - 1
                    }
                ]
            },

            caidan: {
                //显示菜单
                xs: false,
                //判断是文件夹还是文件，
                lb: -1,
                //当前菜单层级
                cj: [] as any,
                caozuo: ["新建文件夹", "新建文章", "新建图表"],
                //激活菜单项
                jh: false
            }
        };
    },
    getters: {},
    actions: {
        getwenben(biaohao: number[]) {
            return this.shuju(biaohao).wenben;
        },
        dakai(biaohao: Array<number>) {
            let shuju = this.shuju(biaohao);
            try {
                this.biaoqian.shuju.forEach((value: any, index: any) => {
                    value.biaohao = JSON.parse(JSON.stringify(value.biaohao));
                    if (value.biaohao.toString() == biaohao.toString()) {
                        this.biaoqian.dqm = shuju.biaoshi;
                        throw new Error("跳出循环");
                    }
                });
                this.biaoqian.shuju.push(JSON.parse(JSON.stringify(shuju)));
                this.biaoqian.dqm = shuju.biaoshi;
            } catch (e) {}
        },
        shuju(biaohao: Array<number>) {
            if (biaohao.length == 1) {
                return this.wenjian.wenjian[biaohao[0] - 1];
            }
            let wenjian = this.wenjian;
            for (let i = 0; i < biaohao.length - 1; i++) {
                wenjian = wenjian.wenjianjia[biaohao[i] - 1];
            }
            return wenjian.wenjian[biaohao[biaohao.length - 1] - 1];
        },
        guanbi(targetName: TabPanelName) {
            const tabs = this.biaoqian.shuju;
            // if (this.biaoqian.dqm === targetName) {
            //   tabs.forEach((tab: { biaoshi: string }, index: number) => {
            //     if (tab.biaoshi === targetName) {
            //       const nextTab = tabs[index + 1] || tabs[index - 1];
            //       if (nextTab) {
            //         this.biaoqian.dqm = nextTab.biaoshi;
            //       }
            //     }
            //   });
            // }
            this.biaoqian.shuju = tabs.filter(
                (tab: { biaoshi: TabPanelName }) => tab.biaoshi !== targetName
            );
            console.log(this.biaoqian.shuju);
        },
        bianji(biaohao: number[], wenben: string) {
            this.shuju(biaohao).wenben = wenben;
            console.log(this.wenjian.wenjian[0]);
        },
        // jihuo(biaoshi: TabsPaneContext): any {
        //   alert(biaoshi);
        // },
        fuzhi() {
            ElMessage({
                message: "复制成功！",
                type: "success",
                duration: 500
            });
        },
        baocun() {
            ElMessage({
                message: "保存成功！",
                type: "success",
                duration: 500
            });
        },
        fabiao() {
            ElMessage({
                message: "发表成功！",
                type: "success",
                duration: 500
            });
        },
        xs() {
            alert(this.biaoqian.shuju[0].wenben);
        },
        xianshi(lb: number, biaohao: Array<number>, e: any) {
            this.caidan.lb = lb;
            this.caidan.cj = biaohao;
            this.caidan.xs = true;
            e.clientX + 183 >= window.innerWidth
                ? (document.getElementById("md-contextMenu")!.style.left = e.clientX - 183 + "px")
                : (document.getElementById("md-contextMenu")!.style.left = e.clientX + "px");
            e.clientY + 228 >= window.innerHeight
                ? (document.getElementById("md-contextMenu")!.style.top = e.clientY - 228 + "px")
                : (document.getElementById("md-contextMenu")!.style.top = e.clientY + "px");
        },
        caozuo(biaohao: number) {
            this.caidan.xs = false;
            this.caidan.jh = !this.caidan.jh;

            if (biaohao == 0) {
                if (this.caidan.lb == -1) {
                    this.wenjian.wenjianjia.push({
                        mingcheng: "文件夹" + (this.wenjian.wenjianjia.length + 1),
                        biaohao: [this.wenjian.wenjianjia.length + 1],
                        biaoshi: String([this.wenjian.wenjianjia.length + 1]),
                        wenjianjia: [],
                        wenjian: []
                    });
                } else if (this.caidan.lb == 0) {
                    let cj = JSON.parse(JSON.stringify(this.caidan.cj));
                    let yuju = "this.wenjian";
                    let mingcheng = "";
                    for (let i = 0; i < cj.length; i++) {
                        yuju += ".wenjianjia[" + (cj[i] - 1) + "]";
                        mingcheng += cj[i] + ",";
                    }

                    mingcheng += eval(yuju + ".wenjianjia.length") + 1;

                    eval(
                        yuju +
                            ".wenjianjia.push({mingcheng:'文件夹" +
                            mingcheng +
                            "',biaohao:[" +
                            mingcheng +
                            "],wenjianjia: [],wenjian:[]});"
                    );
                } else if (this.caidan.lb == 1) {
                    let cj = JSON.parse(JSON.stringify(this.caidan.cj));
                    cj.pop();
                    let yuju = "this.wenjian";
                    let mingcheng = "";
                    for (let i = 0; i < cj.length; i++) {
                        yuju += ".wenjianjia[" + (cj[i] - 1) + "]";
                        mingcheng += cj[i] + ",";
                    }

                    mingcheng += eval(yuju + ".wenjianjia.length") + 1;
                    eval(
                        yuju +
                            ".wenjianjia.push({mingcheng:'文件夹" +
                            mingcheng +
                            "',biaohao:[" +
                            mingcheng +
                            "],wenjianjia: [],wenjian:[]});"
                    );
                }
            } else if (biaohao == 1) {
                if (this.caidan.lb == -1) {
                    this.wenjian.wenjian.push({
                        mingcheng: "文件" + (this.wenjian.wenjian.length + 1),
                        biaohao: [this.wenjian.wenjian.length + 1],
                        biaoshi: "[" + (this.wenjian.wenjian.length + 1) + "]",
                        wenben: "",
                        shijian: 2000 - 1 - 1,
                        zhuangtai: false
                    });
                } else if (this.caidan.lb == 0) {
                    let cj = JSON.parse(JSON.stringify(this.caidan.cj));
                    let yuju = "this.wenjian";
                    let mingcheng = "";
                    for (let i = 0; i < cj.length; i++) {
                        yuju += ".wenjianjia[" + (cj[i] - 1) + "]";
                        mingcheng += cj[i] + ",";
                    }
                    mingcheng += eval(yuju + ".wenjian.length") + 1;

                    eval(
                        yuju +
                            ".wenjian.push({mingcheng:'文件" +
                            mingcheng +
                            "',biaohao:[" +
                            mingcheng +
                            "],biaoshi: '[" +
                            mingcheng +
                            "]',wenben:'',shijian:2000-1-1,zhuangtai:false});"
                    );
                } else if (this.caidan.lb == 1) {
                    let cj = JSON.parse(JSON.stringify(this.caidan.cj));
                    cj.pop();
                    let yuju = "this.wenjian";
                    let mingcheng = "";
                    for (let i = 0; i < cj.length; i++) {
                        yuju += ".wenjianjia[" + (cj[i] - 1) + "]";
                        mingcheng += cj[i] + ",";
                    }
                    mingcheng += eval(yuju + ".wenjian.length") + 1;
                    eval(
                        yuju +
                            ".wenjian.push({mingcheng:'文件" +
                            mingcheng +
                            "',biaohao:[" +
                            mingcheng +
                            "],biaoshi: '[" +
                            mingcheng +
                            "]',wenben:'',shijian:2000-1-1,zhuangtai:false});"
                    );
                }
            }
        },
        huoqushuju() {}
    }
});
